package com.fas.hehe.system.controller;

import java.util.Date;

import javax.sound.midi.Sequence;

import nl.bitwalker.useragentutils.UserAgent;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;

import com.fas.core.BaseController;
import com.fas.core.plugin.jfinal.route.ControllerBind;
import com.fas.core.util.IPUtil;
import com.fas.hehe.log.model.LoginLog;
import com.jfinal.kit.StrKit;

@ControllerBind(controllerKey="/login")
public class LoginController extends BaseController {

	public void index() {
		String exceptionClassName =this.getRequest().getAttribute("shiroLoginFailure")+"";
		String error = null;
		if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
			error = "用户名/密码错误";
		} else if (IncorrectCredentialsException.class.getName().equals(
				exceptionClassName)) {
			error = "用户名/密码错误";
		}else if (StrKit.notBlank(exceptionClassName) ) {
			error = "其他错误：" + exceptionClassName;
		}
		
		if(StrKit.notNull(error)){
			this.setAttr("error", error);
			this.render("/page/login.jsp");
		}else{
			this.redirect("/index");
		}
	}

	public void saveLoginLog(){
		/*//记录登录日志
        LoginLog loginLog = new LoginLog();
        loginLog.set("log_id", Sequence.getSequence());
        loginLog.set("organization_id", corpStaff.getLong("corpid"));
        loginLog.set("name", corpStaff.getStr("name"));
        loginLog.set("account", userAccount);
        loginLog.set("logintime", new Date());
        loginLog.set("ip", IPUtil.getIpAddr( this.getRequest()));
        
        UserAgent userAgent = UserAgent.parseUserAgentString(this.getRequest().getHeader("User-Agent"));  
        loginLog.set("browser",  userAgent.getBrowser().getName());
        loginLog.set("version",  userAgent.getBrowserVersion().getMajorVersion());
        loginLog.set("os", userAgent.getOperatingSystem().getName());
        loginLog.set("devicetype", userAgent.getOperatingSystem().getDeviceType().toString());
        
        loginLog.save();*/
	}
}
